package game.components;


import game.fury.FuryScreen;
import sheep.game.State;
import sheep.math.Vector2;
/**
 *	This class checks for collisions between balls, bricks and padles
 * @author Erlend Heimark
 */
public class Collision extends State {
	
	/**
	 * Turns the balls around when it has hit a padle
	 * @param padle The padle that is hit
	 * @param ball	The ball hitting the padle
	 * @param con	link Constants
	 */
	public static void padelBallCollision(Padle padle, Balls ball, Constants con) {

				Vector2 ballSpeed = ball.getSpeed();
				Vector2 ballPos = ball.getPosition();
				
				if(ballSpeed.getX()>0){
					if((ballPos.getX()-padle.getPosition().getX()) < 0){
						ball.setSpeed((ballSpeed.getX() - ((padle.getPosition().getX()-ballPos.getX())*con.speedChange)), -ballSpeed.getY());
						ball.setPosition(ballPos.getX(),(ballPos.getY()-4));
					}
					else{	

						ball.setSpeed(ballSpeed.getX() + ((ballPos.getX()-padle.getPosition().getX())*con.speedChange), -ballSpeed.getY());
						ball.setPosition(ballPos.getX(),(ballPos.getY()-4));

					}
				}
				else{

					if((ballPos.getX()-padle.getPosition().getX()) < 0){
						ball.setSpeed(ballSpeed.getX() - ((padle.getPosition().getX()-ballPos.getX())*con.speedChange), -ballSpeed.getY());
						ball.setPosition(ballPos.getX(),(ballPos.getY()-4));

					}
					else{	

						ball.setSpeed(ballSpeed.getX() + ((ballPos.getX()-padle.getPosition().getX())*con.speedChange), -ballSpeed.getY());
						ball.setPosition(ballPos.getX(),(ballPos.getY()-4));

					}
				}
	}
	
	/**
	 * Uses a powerUp when it has hit a padle
	 * @param powerUp The poweUp
	 * @param furyScreen The game object
	 */
	public static void padlePowerUpCollision(PowerUp powerUp, FuryScreen furyScreen){
				furyScreen.removeFromColLayer(powerUp);
				Update.usePowerUp(powerUp, furyScreen);
				
	}

	/**
	 * Hadles a collision between a ball and a brick
	 * @param ball The ball hitting
	 * @param brick	The brick beeing hit
	 * @param furyScreen The game object
	 */
	public static void brickCollision(Balls ball, Brick brick, FuryScreen furyScreen) {
		int limit = 41;
		
		Vector2 ballPos = ball.getPosition();
		Vector2 ballSpeed = ball.getSpeed();
		Vector2 brickPos = brick.getPosition();
		boolean bug = true;
	
		if(!ball.isChuck()){
			
			if((brickPos.getX() - ballPos.getX() > limit)){
				if(ballSpeed.getX()>0){
					ball.setSpeed(-ballSpeed.getX(), ballSpeed.getY());
					ball.setPosition((ballPos.getX()-4),(ballPos.getY()));
				}else{
					ball.setPosition((ballPos.getX()-4),(ballPos.getY()));
					bug = false;
				}

				
			}else if((ballPos.getX() - brickPos.getX() > limit)){

				if(ballSpeed.getX()<0){
					ball.setSpeed(-ballSpeed.getX(), ballSpeed.getY());
					ball.setPosition((ballPos.getX()+4),(ballPos.getY()));
				}else{
					ball.setPosition((ballPos.getX()+4),(ballPos.getY()));
					bug = false;
				}
			}
			
			else if((ballPos.getY() < brickPos.getY()) && (brickPos.getX() - ballPos.getX() < limit) && (ballPos.getX() - brickPos.getX() < limit)) {

				if(ballSpeed.getY()>0){
					ball.setSpeed(ballSpeed.getX(), -ballSpeed.getY());
					ball.setPosition(ballPos.getX(),(ballPos.getY()+4));
				}else{
					ball.setPosition(ballPos.getX(),(ballPos.getY()+4));
					bug = false;
				}
			}
			else if(ballPos.getY() > brickPos.getY() && (brickPos.getX() - ballPos.getX() < limit) && (ballPos.getX() - brickPos.getX() < limit)) {
					
				if(ballSpeed.getY()<0){
					ball.setSpeed(ballSpeed.getX(), -ballSpeed.getY());
					ball.setPosition(ballPos.getX(),(ballPos.getY()-4));
				}else{
					ball.setPosition(ballPos.getX(),(ballPos.getY()-4));
					bug = false;
				}
			}
		}
		
		brick.setBrickCount(brick.getBrickCount() - ((Balls) ball).getBallPower());
		if(bug){
			if(brick.getBrickCount()<0){
				// adds a power up 
				if(brick.getPowerUp()) Update.addPowerUp(brickPos, furyScreen);
				furyScreen.colLayer.removeSprite(brick);
				furyScreen.bricks.remove(brick);
				Score.levelScore++;	
			}else{
				Update.setColor(brick, furyScreen);
			}

			if(furyScreen.bricks.size()==0){
				Score.setLevelScore( Score.getLevelScore()*(Score.getBalls()+1)) ;
				Score.setTotalScore(Score.getTotalScore()+Score.getLevelScore());
				furyScreen.gameWon = true;
				
			}
		}	
		
	}

}
